package com.lishe.lishe.retrofit.presenter;

import android.text.TextUtils;
import android.util.Log;

import com.lishe.lishe.lishe.common.Globals;
import com.lishe.lishe.lishe.utils.FileUtils;
import com.lishe.lishe.lishe.utils.NetWorkUtils;
import com.lishe.lishe.retrofit.BaseRequestBody;
import com.lishe.lishe.retrofit.DataResponseListener;
import com.lishe.lishe.retrofit.MyObserver.FileUploadObserver;
import com.lishe.lishe.retrofit.MyRequestBody.UploadFileRequestBody;
import com.lishe.lishe.retrofit.RetrofitFactory;
import com.lishe.lishe.retrofit.bean.UploadInfo;
import com.lishe.lishe.retrofit.bean.WXPayObject;
import com.lishe.lishe.retrofit.bean.WXPayOrderQueryObject;
import com.lishe.lishe.retrofit.interfaces.FileService;
import com.lishe.lishe.retrofit.interfaces.PayService;
import com.lishe.lishe.retrofit.interfaces.ProgressListener;

import java.io.File;
import java.io.InputStream;

import io.reactivex.Observer;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

/**
 * Created by DELL on 2017/11/22.
 */

public class PayPresenter {
    private static PayPresenter payPresenter;

    private PayPresenter() {

    }

    public static PayPresenter getInstance() {
        if (payPresenter == null) {
            payPresenter = new PayPresenter();
        }
        return payPresenter;
    }

    /**
     * 微信支付预订单
     *
     * @return
     */
    public void getPayPreOrder(String token, int money, String payType, String payName, final DataResponseListener<WXPayObject> listener) {

        if (TextUtils.isEmpty(payName)) {
            payName = "微信";
        }
        if (TextUtils.isEmpty(payType)) {
            payType = "wxpay";
        }

        RetrofitFactory.create().create(PayService.class)
                .getPayPreOrder(token, money, payType, payName)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        if (!NetWorkUtils.isNetworkConnected()) {
                            listener.onNetWorkError();
                        }
                    }
                })
                .subscribe(new Observer<BaseRequestBody<WXPayObject>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseRequestBody<WXPayObject> requestBody) {
                        if (requestBody.getResult() == 100 && requestBody.getErrcode() == 0) {
                            listener.onSuccess(requestBody.getData());
                        } else {
                            listener.onFail(requestBody.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 微信支付预订单
     *
     * @return
     */
    private Disposable mDisposable;

    public void getPayResult(String paymentId, final DataResponseListener<WXPayOrderQueryObject> listener) {

        RetrofitFactory.create().create(PayService.class)
                .getPayResult(paymentId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        if (!NetWorkUtils.isNetworkConnected()) {
                            listener.onNetWorkError();
                        }
                    }
                })
                .subscribe(new Observer<BaseRequestBody<WXPayOrderQueryObject>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        if (mDisposable != null){
                            mDisposable.dispose();
                            mDisposable=null;
                        }
                        mDisposable = d;
                    }

                    @Override
                    public void onNext(BaseRequestBody<WXPayOrderQueryObject> requestBody) {
                        Log.e("TAG","trade_state = "+requestBody.getData().getTrade_state());
                        if (requestBody.getResult() == 100 && requestBody.getErrcode() == 0) {
                            listener.onSuccess(requestBody.getData());
                        } else {
                            listener.onFail(requestBody.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
